#import "DetailTableViewController.h"
#import "NSString+HTML.h"
#import "HJObjManager.h"
#import "HJManagedImageV.h"


typedef enum { SectionHeader, SectionDetail } Sections;
typedef enum { SectionHeaderTitle, SectionHeaderDate, SectionHeaderURL } HeaderRows;
typedef enum { SectionDetailSummary } DetailRows;

@implementation DetailTableViewController

@synthesize item, dateString, summaryString;

#pragma mark -
#pragma mark Initialization

- (id)initWithStyle:(UITableViewStyle)style {
    if ((self = [super initWithStyle:style])) {
		
    }
    return self;
}

#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad 
{	
	// Super
    [super viewDidLoad];

	// Date
	//if (item.summary) 
    //{
        self.dateString = item.summary;
        NSLog(@"SELF.DATESTRING%@",self.dateString);
    //}
        
    /*
    if (item.date) {
		NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
		[formatter setDateStyle:NSDateFormatterMediumStyle];
		[formatter setTimeStyle:NSDateFormatterMediumStyle];
		self.dateString = item.summary;// [formatter stringFromDate:item.date];
        
        NSLog(@"SELF.DATESTRING%@",self.dateString);
        
		[formatter release];
	}
	*/
    //NSLog(@"++++++++");
    
	// Summary
	if (item.summary) {
		self.summaryString = [item.summary stringByConvertingHTMLToPlainText];
	} else {
		self.summaryString = @"[No Summary]";
	}

    objMan = [[HJObjManager alloc] initWithLoadingBufferSize:10 memCacheSize:25];
    
    HJManagedImageV* myimage;
    myimage = [[[HJManagedImageV alloc] initWithFrame:CGRectMake(5,5,100,100)] autorelease];
    myimage.tag = 999;
    //[self.view addSubview:myimage];
        
    myimage.url = [NSURL URLWithString:item.image];
    [objMan manage:myimage];
    
    UITextView *titleTextfield = [[UITextView alloc] initWithFrame:CGRectMake(10,0,340,120)];
    titleTextfield.textAlignment = UITextAlignmentLeft;
    titleTextfield.textColor = [UIColor blackColor]; //text color
    titleTextfield.font = [UIFont systemFontOfSize:18];  //font size   
    titleTextfield.backgroundColor =[UIColor clearColor];
    titleTextfield.autocorrectionType = UITextAutocorrectionTypeNo;	// no auto correction support   
    titleTextfield.text = [[NSString alloc] initWithFormat:@"%@%@\n设计事务所:%@\n摄影师:%@\n",@"所在地:",item.summary,item.agency,item.designer];
    [self.view addSubview:titleTextfield];
    
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 130, 300, 270)];
    webView.backgroundColor = [UIColor whiteColor];
    //webView.delegate = self.view;
    webView.scalesPageToFit = FALSE;
    [self.view addSubview:webView];
    [webView loadHTMLString:item.content baseURL:Nil];
     //NSLog(@"%@",item.content);
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
	switch (section) {
		case 0: return 2;
		default: return 1;
	}
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

    // Get cell
	static NSString *CellIdentifier = @"Cell_Details";
    
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
	}
	
	// Display
	cell.textLabel.textColor = [UIColor blackColor];
	cell.textLabel.font = [UIFont systemFontOfSize:14];
	cell.hidden = TRUE;
    if(FALSE)//if (item) 
    {
		// Item Info
		NSString *itemTitle = item.title ? [item.title stringByConvertingHTMLToPlainText] : @"[No Title]";
		
		// Display
		switch (indexPath.section) {
			case SectionHeader: {
				
				// Header
				switch (indexPath.row) {
					case SectionHeaderTitle:
						cell.textLabel.font = [UIFont boldSystemFontOfSize:16];
						cell.textLabel.text = itemTitle;
						break;
					case SectionHeaderDate:
                        cell.textLabel.font = [UIFont boldSystemFontOfSize:13];
						cell.textLabel.text = dateString ? dateString : @"[No Date]";
                        
						break;
					/*
                    case SectionHeaderURL:
                        cell.textLabel.font = [UIFont boldSystemFontOfSize:12];
						cell.textLabel.text = item.link ? item.link : @"[No Link]";
						cell.textLabel.textColor = [UIColor blueColor];
						cell.selectionStyle = UITableViewCellSelectionStyleBlue;
						break;
                    */
				}
				break;
				
			}
			case SectionDetail: 
            {
				cell.textLabel.text = summaryString;    // Summary
				cell.textLabel.numberOfLines = 0;       // Multiline
				break;
			}
		}
	}
    
    return cell;
	
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	if (indexPath.section == SectionHeader) {
		return 34;
	} else {
		// Get height
		NSString *summary = @"[No Summary]";
		if (summaryString) summary = summaryString;
		CGSize s = [summary sizeWithFont:[UIFont systemFontOfSize:14] 
					   constrainedToSize:CGSizeMake(self.view.bounds.size.width - 40, MAXFLOAT)  //cell padding
						   lineBreakMode:UILineBreakModeWordWrap];
		return s.height + 16; // Add padding
		
	}
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

	// Open URL
	if (indexPath.section == SectionHeader && indexPath.row == SectionHeaderURL) {
		if (item.link) {
			[[UIApplication sharedApplication] openURL:[NSURL URLWithString:item.link]];
		}
	}
	
	// Deselect
	[self.tableView deselectRowAtIndexPath:indexPath animated:YES];

}

#pragma mark -
#pragma mark Memory management

- (void)dealloc {
	[dateString release];
	[summaryString release];
	[item release];
    [super dealloc];
}

@end

